<!DOCTYPE html>
<html lang=zh>
<head>
    <meta charset="utf-8">
    
    <title>Docker 部署 Redis5.0 集群，以及 ioredis 连接集群 | Tyrival</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="Redis的高可用模式分为两种——集群模式和哨兵模式，本文主要介绍基于docker部署集群的方法。">
<meta name="keywords" content="server,database">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker 部署 Redis5.0 集群，以及 ioredis 连接集群">
<meta property="og:url" content="http://tyrival.github.io/posts/redis-cluster-by-docker/index.html">
<meta property="og:site_name" content="Tyrival">
<meta property="og:description" content="Redis的高可用模式分为两种——集群模式和哨兵模式，本文主要介绍基于docker部署集群的方法。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-02-18T06:40:36.937Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Docker 部署 Redis5.0 集群，以及 ioredis 连接集群">
<meta name="twitter:description" content="Redis的高可用模式分为两种——集群模式和哨兵模式，本文主要介绍基于docker部署集群的方法。">
    

    

    
        <link rel="icon" href="/css/images/logo.png" />
    

    <link rel="stylesheet" href="/libs/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/libs/open-sans/styles.css">
    <link rel="stylesheet" href="/libs/source-code-pro/styles.css">

    <link rel="stylesheet" href="/css/style.css">

    <script src="/libs/jquery/2.1.3/jquery.min.js"></script>
    
    
        <link rel="stylesheet" href="/libs/lightgallery/css/lightgallery.min.css">
    
    
        <link rel="stylesheet" href="/libs/justified-gallery/justifiedGallery.min.css">
    
    
    
    


</head>

<body>
    <div id="container">
        <header id="header">
    <div id="header-main" class="header-inner">
        <div class="outer">
            <a href="/" id="logo">
                <i class="logo"></i>
                <span class="site-title">Tyrival</span>
            </a>
            <nav id="main-nav">
                
                    <a class="main-nav-link" href="http://www.tyrival.com">主页</a>
                
                    <a class="main-nav-link" href="/categories">分类</a>
                
                    <a class="main-nav-link" href="/tags">标签</a>
                
            </nav>
            
                
                <nav id="sub-nav">
                    <div class="profile" id="profile-nav">
                        <a id="profile-anchor" href="javascript:;">
                            <img class="avatar" src="/css/images/avatar.png" />
                            <i class="fa fa-caret-down"></i>
                        </a>
                    </div>
                </nav>
            
            <div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="搜索" />
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="想要查找什么..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>

</div>
        </div>
    </div>
    <div id="main-nav-mobile" class="header-sub header-inner">
        <table class="menu outer">
            <tr>
                
                    <td><a class="main-nav-link" href="http://www.tyrival.com">主页</a></td>
                
                    <td><a class="main-nav-link" href="/categories">分类</a></td>
                
                    <td><a class="main-nav-link" href="/tags">标签</a></td>
                
                <td>
                    
    <div class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="搜索" />
    </div>

                </td>
            </tr>
        </table>
    </div>
</header>

        <div class="outer">
            
                

<aside id="profile">
    <div class="inner profile-inner">
        <div class="base-info profile-block">
            <img id="avatar" src="/css/images/avatar.png" />
            <h2 id="name">周晨煜</h2>
            <h3 id="title">程序员 &amp; 自学狂</h3>
            <span id="location"><i class="fa fa-map-marker"></i>无锡, 中国</span>
            <a id="follow" target="_blank" href="https://github.com/tyrival">关注我</a>
        </div>
        <div class="article-info profile-block">
            <div class="article-info-block">
                45
                <span>文章</span>
            </div>
            <div class="article-info-block">
                19
                <span>标签</span>
            </div>
        </div>
        
        <div class="profile-block social-links">
            <table>
                <tr>
                    
                    
                    <td>
                        <a href="https://github.com/tyrival" target="_blank" title="github" class=tooltip>
                            <i class="fa fa-github"></i>
                        </a>
                    </td>
                    
                    <td>
                        <a href="https://twitter.com/Tyrival" target="_blank" title="twitter" class=tooltip>
                            <i class="fa fa-twitter"></i>
                        </a>
                    </td>
                    
                </tr>
            </table>
        </div>
        
    </div>
</aside>

            
            <section id="main"><article id="post-redis-cluster-by-docker" class="article article-type-post" itemscope itemprop="blogPost">
    <div class="article-inner">
        
        
            <header class="article-header">
                
    
        <h1 class="article-title" itemprop="name">
            Docker 部署 Redis5.0 集群，以及 ioredis 连接集群
        </h1>
    

                
                    <div class="article-meta">
                        
    <div class="article-date">
        <i class="fa fa-calendar"></i>
        <a href="/posts/redis-cluster-by-docker/">
            <time datetime="2019-02-18T05:20:49.552Z" itemprop="datePublished">2019-02-18</time>
        </a>
    </div>


                        
    <div class="article-category">
    	<i class="fa fa-folder"></i>
        <a class="article-category-link" href="/categories/系统/">系统</a>
    </div>

                        
    <div class="article-tag">
        <i class="fa fa-tag"></i>
        <a class="tag-link" href="/tags/database/">database</a>, <a class="tag-link" href="/tags/server/">server</a>
    </div>

                    </div>
                
            </header>
        
        
        <div class="article-entry" itemprop="articleBody">
        
            
            <p>Redis的高可用模式分为两种——集群模式和哨兵模式，本文主要介绍基于docker部署集群的方法。</p>
<a id="more"></a>
<ul>
<li>此处使用docker compose进行部署，首先需要下载redis镜像</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull redis</span><br></pre></td></tr></table></figure>
<ul>
<li>创建redis集群文件夹</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 假设集群的slot分配为3份，副本数为1，则共需要6个节点</span></span><br><span class="line"><span class="comment"># 创建7000, 7001, 7002, 7003, 7004, 7005 共6个文件夹</span></span><br><span class="line">mkdir 7000</span><br></pre></td></tr></table></figure>
<ul>
<li>在以上6个文件夹中分别创建redis配置文件，其中端口号根据节点文件夹名称保持一致</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"># 文件名：redis.conf</span><br><span class="line">## 节点端口：7000, 7001, 7002, 7003, 7004, 7005</span><br><span class="line">port 7000</span><br><span class="line"></span><br><span class="line">## cluster集群模式</span><br><span class="line">cluster-enabled yes</span><br><span class="line"></span><br><span class="line">## 集群配置名</span><br><span class="line">cluster-config-file nodes.conf</span><br><span class="line"></span><br><span class="line">## 关闭保护模式</span><br><span class="line">#protected-mode no</span><br><span class="line"></span><br><span class="line">## 超时时间</span><br><span class="line">cluster-node-timeout 5000</span><br><span class="line"></span><br><span class="line">## 持久化模式</span><br><span class="line">appendonly yes</span><br></pre></td></tr></table></figure>
<ul>
<li>创建集群的docker网络</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 网络名称redis-cluster，下一步用到，默认使用bridge模式</span></span><br><span class="line">docker network redis-cluster</span><br></pre></td></tr></table></figure>
<ul>
<li>在集群文件夹根目录下创建docker compose配置文件</li>
</ul>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 文件名：docker-compose.yml</span></span><br><span class="line"><span class="attr">redis-7000:</span></span><br><span class="line">  <span class="comment"># 基于redis镜像构建</span></span><br><span class="line"><span class="attr">  image:</span> <span class="string">redis</span></span><br><span class="line">  <span class="comment"># 使用上一步创建的docker网络</span></span><br><span class="line"><span class="attr">  net:</span> <span class="string">"redis-cluster"</span></span><br><span class="line"><span class="attr">  restart:</span> <span class="string">always</span></span><br><span class="line"><span class="attr">  privileged:</span> <span class="literal">true</span></span><br><span class="line">  <span class="comment"># 对外暴露redis连接端口7000，以及集群间相互通讯的端口17000，通讯端口=连接端口+10000</span></span><br><span class="line"><span class="attr">  ports:</span></span><br><span class="line"><span class="bullet">    -</span> <span class="number">7000</span><span class="string">:7000</span></span><br><span class="line"><span class="bullet">    -</span> <span class="number">17000</span><span class="string">:17000</span></span><br><span class="line">  <span class="comment"># 映射配置文件和数据存储文件夹到redis节点容器内部</span></span><br><span class="line"><span class="attr">  volumes:</span></span><br><span class="line"><span class="bullet">    -</span> <span class="string">/redis-cluster/7000/redis.conf:/etc/redis/redis.conf</span></span><br><span class="line"><span class="bullet">    -</span> <span class="string">/redis-cluster/7000/data:/data</span></span><br><span class="line">  <span class="comment"># 默认执行命令，使用redis.conf启动redis</span></span><br><span class="line"><span class="attr">  command:</span> <span class="string">["redis-server",</span> <span class="string">"/etc/redis/redis.conf"</span><span class="string">]</span></span><br><span class="line"><span class="attr">redis-7001:</span></span><br><span class="line"><span class="attr">  image:</span> <span class="string">redis</span></span><br><span class="line"><span class="attr">  net:</span> <span class="string">"redis-cluster"</span></span><br><span class="line"><span class="attr">  restart:</span> <span class="string">always</span></span><br><span class="line"><span class="attr">  privileged:</span> <span class="literal">true</span></span><br><span class="line"><span class="attr">  ports:</span></span><br><span class="line"><span class="bullet">    -</span> <span class="number">7001</span><span class="string">:7001</span></span><br><span class="line"><span class="bullet">    -</span> <span class="number">17001</span><span class="string">:17001</span></span><br><span class="line"><span class="attr">  volumes:</span></span><br><span class="line"><span class="bullet">    -</span> <span class="string">/redis-cluster/7001/redis.conf:/etc/redis/redis.conf</span></span><br><span class="line"><span class="bullet">    -</span> <span class="string">/redis-cluster/7001/data:/data</span></span><br><span class="line"><span class="attr">  command:</span> <span class="string">["redis-server",</span> <span class="string">"/etc/redis/redis.conf"</span><span class="string">]</span></span><br><span class="line"><span class="attr">redis-7002:</span></span><br><span class="line"><span class="attr">  image:</span> <span class="string">redis</span></span><br><span class="line"><span class="attr">  net:</span> <span class="string">"redis-cluster"</span></span><br><span class="line"><span class="attr">  restart:</span> <span class="string">always</span></span><br><span class="line"><span class="attr">  privileged:</span> <span class="literal">true</span></span><br><span class="line"><span class="attr">  ports:</span></span><br><span class="line"><span class="bullet">    -</span> <span class="number">7002</span><span class="string">:7002</span></span><br><span class="line"><span class="bullet">    -</span> <span class="number">17002</span><span class="string">:17002</span></span><br><span class="line"><span class="attr">  volumes:</span></span><br><span class="line"><span class="bullet">    -</span> <span class="string">/redis-cluster/7002/redis.conf:/etc/redis/redis.conf</span></span><br><span class="line"><span class="bullet">    -</span> <span class="string">/redis-cluster/7002/data:/data</span></span><br><span class="line"><span class="attr">  command:</span> <span class="string">["redis-server",</span> <span class="string">"/etc/redis/redis.conf"</span><span class="string">]</span></span><br><span class="line"><span class="attr">redis-7003:</span></span><br><span class="line"><span class="attr">  image:</span> <span class="string">redis</span></span><br><span class="line"><span class="attr">  net:</span> <span class="string">"redis-cluster"</span></span><br><span class="line"><span class="attr">  restart:</span> <span class="string">always</span></span><br><span class="line"><span class="attr">  privileged:</span> <span class="literal">true</span></span><br><span class="line"><span class="attr">  ports:</span></span><br><span class="line"><span class="bullet">    -</span> <span class="number">7003</span><span class="string">:7003</span></span><br><span class="line"><span class="bullet">    -</span> <span class="number">17003</span><span class="string">:17003</span></span><br><span class="line"><span class="attr">  volumes:</span></span><br><span class="line"><span class="bullet">    -</span> <span class="string">/redis-cluster/7003/redis.conf:/etc/redis/redis.conf</span></span><br><span class="line"><span class="bullet">    -</span> <span class="string">/redis-cluster/7003/data:/data</span></span><br><span class="line"><span class="attr">  command:</span> <span class="string">["redis-server",</span> <span class="string">"/etc/redis/redis.conf"</span><span class="string">]</span></span><br><span class="line"><span class="attr">redis-7004:</span></span><br><span class="line"><span class="attr">  image:</span> <span class="string">redis</span></span><br><span class="line"><span class="attr">  net:</span> <span class="string">"redis-cluster"</span></span><br><span class="line"><span class="attr">  restart:</span> <span class="string">always</span></span><br><span class="line"><span class="attr">  privileged:</span> <span class="literal">true</span></span><br><span class="line"><span class="attr">  ports:</span></span><br><span class="line"><span class="bullet">    -</span> <span class="number">7004</span><span class="string">:7004</span></span><br><span class="line"><span class="bullet">    -</span> <span class="number">17004</span><span class="string">:17004</span></span><br><span class="line"><span class="attr">  volumes:</span></span><br><span class="line"><span class="bullet">    -</span> <span class="string">/redis-cluster/7004/redis.conf:/etc/redis/redis.conf</span></span><br><span class="line"><span class="bullet">    -</span> <span class="string">/redis-cluster/7004/data:/data</span></span><br><span class="line"><span class="attr">  command:</span> <span class="string">["redis-server",</span> <span class="string">"/etc/redis/redis.conf"</span><span class="string">]</span></span><br><span class="line"><span class="attr">redis-7005:</span></span><br><span class="line"><span class="attr">  image:</span> <span class="string">redis</span></span><br><span class="line"><span class="attr">  net:</span> <span class="string">"redis-cluster"</span></span><br><span class="line"><span class="attr">  restart:</span> <span class="string">always</span></span><br><span class="line"><span class="attr">  privileged:</span> <span class="literal">true</span></span><br><span class="line"><span class="attr">  ports:</span></span><br><span class="line"><span class="bullet">    -</span> <span class="number">7005</span><span class="string">:7005</span></span><br><span class="line"><span class="bullet">    -</span> <span class="number">17005</span><span class="string">:17005</span></span><br><span class="line"><span class="attr">  volumes:</span></span><br><span class="line"><span class="bullet">    -</span> <span class="string">/redis-cluster/7005/redis.conf:/etc/redis/redis.conf</span></span><br><span class="line"><span class="bullet">    -</span> <span class="string">/redis-cluster/7005/data:/data</span></span><br><span class="line"><span class="attr">  command:</span> <span class="string">["redis-server",</span> <span class="string">"/etc/redis/redis.conf"</span><span class="string">]</span></span><br></pre></td></tr></table></figure>
<ul>
<li>启动docker容器</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker-compose up -d</span><br></pre></td></tr></table></figure>
<p>启动完成后，查看容器情况</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">docker ps</span><br><span class="line"></span><br><span class="line">CONTAINER ID        IMAGE               COMMAND                  PORTS                                                        NAMES</span><br><span class="line">e3b9b2104d16        redis               <span class="string">"docker-entrypoint.s…"</span>   0.0.0.0:7005-&gt;7005/tcp, 6379/tcp, 0.0.0.0:17005-&gt;17005/tcp   redis-cluster_redis-7005_1</span><br><span class="line">......</span><br></pre></td></tr></table></figure>
<p>查看各容器IP</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">docker network inspect redis-cluster</span><br><span class="line"></span><br><span class="line"><span class="comment">## 可以查看到redis-cluster网络下，各节点的内网IP，示例如下</span></span><br><span class="line"><span class="string">"e3b9b2104d16fef938efde2a82da4a919c3fff456657a3c166f79325ceb3969c"</span>: &#123;</span><br><span class="line">    <span class="string">"Name"</span>: <span class="string">"redis-cluster_redis-7005_1"</span>,</span><br><span class="line">    <span class="string">"EndpointID"</span>: <span class="string">"56467236c42862e73f924371fa2145f91c48b0aa6ccd7304b22e7329f00e32d4"</span>,</span><br><span class="line">    <span class="string">"MacAddress"</span>: <span class="string">"02:42:ac:14:00:07"</span>,</span><br><span class="line">    <span class="comment">## 可以看到7005节点的内网IP是172.20.0.7</span></span><br><span class="line">    <span class="string">"IPv4Address"</span>: <span class="string">"172.20.0.7/16"</span>,</span><br><span class="line">    <span class="string">"IPv6Address"</span>: <span class="string">""</span></span><br><span class="line">&#125;,</span><br><span class="line">......</span><br></pre></td></tr></table></figure>
<ul>
<li>根据上面查到的IP地址构建集群关联命令，并执行，其中需要输入 <code>yes</code> 进行确认</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 按照 ip:端口 的模式拼接6个节点的信息</span></span><br><span class="line">redis-cli --cluster create 172.20.0.2:7000 172.20.0.4:7001 172.20.0.5:7002 172.20.0.6:7003 172.20.0.3:7004 172.20.0.7:7005 --cluster-replicas 1</span><br></pre></td></tr></table></figure>
<ul>
<li>测试集群情况</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">redis-cli -p 7000 cluster nodes</span><br><span class="line"></span><br><span class="line"><span class="comment"># 出现以下信息表示集群构建成功</span></span><br><span class="line">1c5811fde15d56bbbbf68aa55b6b6a6523a7781a 172.20.0.6:7003@17003 slave b9697bec499b47020753397f99d5eb0fb1340616 0 1550471531098 4 connected</span><br><span class="line">b9697bec499b47020753397f99d5eb0fb1340616 172.20.0.2:7000@17000 myself,master - 0 1550471530000 1 connected 0-5460</span><br><span class="line">4b37f8c44aeaab184b423926a384081e27d2d97d 172.20.0.7:7005@17005 slave e991cb1601d557f97fb89b2a8eee3daaa479ef67 0 1550471530000 6 connected</span><br><span class="line">e991cb1601d557f97fb89b2a8eee3daaa479ef67 172.20.0.5:7002@17002 master - 0 1550471529880 3 connected 10923-16383</span><br><span class="line">42ddc0966cc80b9795bf45f4f826c93991db0af2 172.20.0.4:7001@17001 master - 0 1550471529000 2 connected 5461-10922</span><br><span class="line">c11ea451ba7fd6ae21e8ce6043981fa3f2aaddaa 172.20.0.3:7004@17004 slave 42ddc0966cc80b9795bf45f4f826c93991db0af2 0 1550471530896 5 connected</span><br></pre></td></tr></table></figure>
<ul>
<li>ioredis</li>
</ul>
<p>集群构建成功后，如果使用ioredis连接，需要声明natMap属性，代码如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Redis <span class="keyword">from</span> <span class="string">'ioredis'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 外部地址和端口</span></span><br><span class="line"><span class="keyword">let</span> cluster = [</span><br><span class="line">    &#123;<span class="attr">host</span>: <span class="string">'localhost'</span>, <span class="attr">port</span>: <span class="number">7000</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">host</span>: <span class="string">'localhost'</span>, <span class="attr">port</span>: <span class="number">7001</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">host</span>: <span class="string">'localhost'</span>, <span class="attr">port</span>: <span class="number">7002</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">host</span>: <span class="string">'localhost'</span>, <span class="attr">port</span>: <span class="number">7003</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">host</span>: <span class="string">'localhost'</span>, <span class="attr">port</span>: <span class="number">7004</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">host</span>: <span class="string">'localhost'</span>, <span class="attr">port</span>: <span class="number">7005</span>&#125;</span><br><span class="line">]</span><br><span class="line"><span class="keyword">let</span> options = &#123;</span><br><span class="line">    <span class="comment">// 声明每个内网 IP:端口 对应的外部地址和端口</span></span><br><span class="line">    natMap: &#123;</span><br><span class="line">        <span class="string">'172.20.0.2:7000'</span>: &#123;<span class="attr">host</span>: <span class="string">'localhost'</span>, <span class="attr">port</span>: <span class="number">7000</span>&#125;,</span><br><span class="line">    	<span class="string">'172.20.0.4:7001'</span>: &#123;<span class="attr">host</span>: <span class="string">'localhost'</span>, <span class="attr">port</span>: <span class="number">7001</span>&#125;,</span><br><span class="line">    	<span class="string">'172.20.0.5:7002'</span>: &#123;<span class="attr">host</span>: <span class="string">'localhost'</span>, <span class="attr">port</span>: <span class="number">7002</span>&#125;,</span><br><span class="line">    	<span class="string">'172.20.0.6:7003'</span>: &#123;<span class="attr">host</span>: <span class="string">'localhost'</span>, <span class="attr">port</span>: <span class="number">7003</span>&#125;,</span><br><span class="line">    	<span class="string">'172.20.0.3:7004'</span>: &#123;<span class="attr">host</span>: <span class="string">'localhost'</span>, <span class="attr">port</span>: <span class="number">7004</span>&#125;,</span><br><span class="line">    	<span class="string">'172.20.0.7:7005'</span>: &#123;<span class="attr">host</span>: <span class="string">'localhost'</span>, <span class="attr">port</span>: <span class="number">7005</span>&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> client = <span class="keyword">new</span> Redis.Cluster(cluster, options)</span><br></pre></td></tr></table></figure>

        
        </div>
        <footer class="article-footer">
            <div class="share-container">



</div>

    <a data-url="http://tyrival.github.io/posts/redis-cluster-by-docker/" data-id="cjs9z0xpo002r5aa7qx2vqaif" class="article-share-link"><i class="fa fa-share"></i>分享到</a>
<script>
    (function ($) {
        // Prevent duplicate binding
        if (typeof(__SHARE_BUTTON_BINDED__) === 'undefined' || !__SHARE_BUTTON_BINDED__) {
            __SHARE_BUTTON_BINDED__ = true;
        } else {
            return;
        }
        $('body').on('click', function() {
            $('.article-share-box.on').removeClass('on');
        }).on('click', '.article-share-link', function(e) {
            e.stopPropagation();

            var $this = $(this),
                url = $this.attr('data-url'),
                encodedUrl = encodeURIComponent(url),
                id = 'article-share-box-' + $this.attr('data-id'),
                offset = $this.offset(),
                box;

            if ($('#' + id).length) {
                box = $('#' + id);

                if (box.hasClass('on')){
                    box.removeClass('on');
                    return;
                }
            } else {
                var html = [
                    '<div id="' + id + '" class="article-share-box">',
                        '<input class="article-share-input" value="' + url + '">',
                        '<div class="article-share-links">',
                            '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="fa fa-twitter article-share-twitter" target="_blank" title="Twitter"></a>',
                            '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="fa fa-facebook article-share-facebook" target="_blank" title="Facebook"></a>',
                            '<a href="http://pinterest.com/pin/create/button/?url=' + encodedUrl + '" class="fa fa-pinterest article-share-pinterest" target="_blank" title="Pinterest"></a>',
                            '<a href="https://plus.google.com/share?url=' + encodedUrl + '" class="fa fa-google article-share-google" target="_blank" title="Google+"></a>',
                        '</div>',
                    '</div>'
                ].join('');

              box = $(html);

              $('body').append(box);
            }

            $('.article-share-box.on').hide();

            box.css({
                top: offset.top + 25,
                left: offset.left
            }).addClass('on');

        }).on('click', '.article-share-box', function (e) {
            e.stopPropagation();
        }).on('click', '.article-share-box-input', function () {
            $(this).select();
        }).on('click', '.article-share-box-link', function (e) {
            e.preventDefault();
            e.stopPropagation();

            window.open(this.href, 'article-share-box-window-' + Date.now(), 'width=500,height=450');
        });
    })(jQuery);
</script>

            
    

        </footer>
    </div>
    
        
<nav id="article-nav">
    
    
        <a href="/posts/hadoop-docker-01/" id="article-nav-older" class="article-nav-link-wrap">
            <strong class="article-nav-caption">下一篇</strong>
            <div class="article-nav-title">Docker+Hadoop 01：环境部署</div>
        </a>
    
</nav>


    
</article>


    
    
        <section id="comments">
	<div id="commentContainer"></div>
</section>
    

</section>
            
                
<aside id="sidebar">
   
        
    <div class="widget-wrap">
        <h3 class="widget-title">最新文章</h3>
        <div class="widget">
            <ul id="recent-post" class="no-thumbnail">
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/系统/">系统</a></p>
                            <p class="item-title"><a href="/posts/redis-cluster-by-docker/" class="title">Docker 部署 Redis5.0 集群，以及 ioredis 连接集群</a></p>
                            <p class="item-date"><time datetime="2019-02-18T05:20:49.552Z" itemprop="datePublished">2019-02-18</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/hadoop/">hadoop</a></p>
                            <p class="item-title"><a href="/posts/hadoop-docker-01/" class="title">Docker+Hadoop 01：环境部署</a></p>
                            <p class="item-date"><time datetime="2019-01-02T23:58:03.975Z" itemprop="datePublished">2019-01-03</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/后端/">后端</a></p>
                            <p class="item-title"><a href="/posts/springboot-tesseract/" class="title">SpringBoot05：Spring Boot与Tesseract集成</a></p>
                            <p class="item-date"><time datetime="2018-07-08T06:26:12.000Z" itemprop="datePublished">2018-07-08</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/后端/">后端</a></p>
                            <p class="item-title"><a href="/posts/springboot-elastic/" class="title">SpringBoot04：基于Docker和Spring Boot部署开发Elastic</a></p>
                            <p class="item-date"><time datetime="2018-07-08T06:25:55.000Z" itemprop="datePublished">2018-07-08</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/后端/">后端</a></p>
                            <p class="item-title"><a href="/posts/springboot-solr/" class="title">SpringBoot03：基于Docker和Spring Boot部署开发Solr</a></p>
                            <p class="item-date"><time datetime="2018-07-08T06:25:50.000Z" itemprop="datePublished">2018-07-08</time></p>
                        </div>
                    </li>
                
            </ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">分类</h3>
        <div class="widget">
            <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/hadoop/">hadoop</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/iOS/">iOS</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/前端/">前端</a><span class="category-list-count">14</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/后端/">后端</a><span class="category-list-count">8</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/工具/">工具</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/服务器/">服务器</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/硬件/">硬件</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/算法/">算法</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/系统/">系统</a><span class="category-list-count">4</span></li></ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">标签</h3>
        <div class="widget">
            <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/algorithm/">algorithm</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/cloud/">cloud</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/css/">css</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/database/">database</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/">docker</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/f-k-ie/">f**k ie</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/">git</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hadoop/">hadoop</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/">java</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/javascript/">javascript</a><span class="tag-list-count">12</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/less/">less</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mac/">mac</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/">python</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/raspberry/">raspberry</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/server/">server</a><span class="tag-list-count">12</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/swift/">swift</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/tomcat/">tomcat</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vue/">vue</a><span class="tag-list-count">2</span></li></ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">标签云</h3>
        <div class="widget tagcloud">
            <a href="/tags/algorithm/" style="font-size: 14px;">algorithm</a> <a href="/tags/cloud/" style="font-size: 10px;">cloud</a> <a href="/tags/css/" style="font-size: 12px;">css</a> <a href="/tags/database/" style="font-size: 14px;">database</a> <a href="/tags/docker/" style="font-size: 10px;">docker</a> <a href="/tags/f-k-ie/" style="font-size: 14px;">f**k ie</a> <a href="/tags/git/" style="font-size: 12px;">git</a> <a href="/tags/hadoop/" style="font-size: 18px;">hadoop</a> <a href="/tags/java/" style="font-size: 16px;">java</a> <a href="/tags/javascript/" style="font-size: 20px;">javascript</a> <a href="/tags/less/" style="font-size: 10px;">less</a> <a href="/tags/linux/" style="font-size: 14px;">linux</a> <a href="/tags/mac/" style="font-size: 10px;">mac</a> <a href="/tags/python/" style="font-size: 10px;">python</a> <a href="/tags/raspberry/" style="font-size: 12px;">raspberry</a> <a href="/tags/server/" style="font-size: 20px;">server</a> <a href="/tags/swift/" style="font-size: 10px;">swift</a> <a href="/tags/tomcat/" style="font-size: 10px;">tomcat</a> <a href="/tags/vue/" style="font-size: 12px;">vue</a>
        </div>
    </div>

    
        
    <div class="widget-wrap widget-list">
        <h3 class="widget-title">链接</h3>
        <div class="widget">
            <ul>
                
            </ul>
        </div>
    </div>


    
    <div id="toTop" class="fa fa-angle-up"></div>
</aside>

            
        </div>
        <footer id="footer">
    <div class="outer">
        <div id="footer-info" class="inner">
            &copy; 2019 Tyrival<br>
            Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>. Theme by <a href="http://github.com/ppoffice">PPOffice</a>
        </div>
    </div>
</footer>
        


    
        <script src="/libs/lightgallery/js/lightgallery.min.js"></script>
        <script src="/libs/lightgallery/js/lg-thumbnail.min.js"></script>
        <script src="/libs/lightgallery/js/lg-pager.min.js"></script>
        <script src="/libs/lightgallery/js/lg-autoplay.min.js"></script>
        <script src="/libs/lightgallery/js/lg-fullscreen.min.js"></script>
        <script src="/libs/lightgallery/js/lg-zoom.min.js"></script>
        <script src="/libs/lightgallery/js/lg-hash.min.js"></script>
        <script src="/libs/lightgallery/js/lg-share.min.js"></script>
        <script src="/libs/lightgallery/js/lg-video.min.js"></script>
    
    
        <script src="/libs/justified-gallery/jquery.justifiedGallery.min.js"></script>
    
    
        <script type="text/x-mathjax-config">
            MathJax.Hub.Config({ tex2jax: { inlineMath: [['$','$'], ['\\(','\\)']] } });
        </script>
        <script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"></script>
    



<!-- Custom Scripts -->
<script src="/js/main.js"></script>

    </div>
</body>
</html>